package org.example.cet.demos.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.example.cet.demos.converter.converter;
import org.example.cet.demos.domain.PageBean;
import org.example.cet.demos.domain.Result;
import org.example.cet.demos.domain.form.StudentForm;
import org.example.cet.demos.domain.po.Student;
import org.example.cet.demos.domain.query.StudentQuery;
import org.example.cet.demos.service.impl.StudentServiceImpl;
import org.example.cet.demos.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.sql.Wrapper;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hq
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentServiceImpl studentService;
    @Autowired
    private converter converter;

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/wxLogin")
    public Result getUserInfoRest(String code) {
        String id = "wx668c24af28aa40ed";
        String secret = "2bf7d17e5eccac47fa67d1e0ca688b6c";
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + id + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
        System.out.println(url);
        ResponseEntity<String> result = restTemplate.getForEntity(url, String.class);
        System.out.println(result.getBody());
        Map map = JSON.parseObject(result.getBody(), Map.class);
        Student one = studentService.getById(map.get("openid").toString());
        if ( one != null){
            return Result.success(one.getId()+ " " + one.getName());
        }
        Student student = new Student();
        student.setId(map.get("openid").toString());
        student.setName("超爱英语");
        studentService.save(student);
        return Result.success(student.getId()+ " " + student.getName());
    }


    @PostMapping("/add")
    public Result add(@Valid @RequestBody StudentForm studentForm) {
        Student student=converter.ToStudent(studentForm);
        studentService.save(student);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        studentService.removeById(id);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result byId(@PathVariable String id) {
        return Result.success(studentService.getById(id));
    }

    @PutMapping("/update/{id}")
    public Result update(@PathVariable String id,@Valid @RequestBody StudentForm studentForm) {
        Student student=converter.ToStudent(studentForm);
        student.setId(id);
        studentService.updateById(student);
        return Result.success();
    }

    @GetMapping("/getPage")
    public Result getPage(StudentQuery studentQuery) {
        PageBean pb=studentService.getPage(studentQuery);
        return Result.success(pb);
    }

    @GetMapping("/getList")
    public Result getList(){
        return Result.success(studentService.getList());
    }

}
